package cn.kgc.crowdfundingadmin02.controller;

import cn.kgc.crowdfundingadmin02.common.ResultEntity;
import cn.kgc.crowdfundingadmin02.pojo.Role;
import cn.kgc.crowdfundingadmin02.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-26 9:35
 */
@Controller
public class RoleController {


    @Resource
    RoleService roleService;

    //分页查询数据
    @ResponseBody
    @RequestMapping("/role/search/by/keyword")
    public ResultEntity<PageInfo<Role>> serach(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize,
            @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword
    ) {
        //1.查询得到PageInfo对象
        PageInfo<Role> pageInfo = roleService.queryForKeywordWithPage(pageNum, pageSize, keyword);
//封装结果返回对象
        return ResultEntity.successWithoutData(pageInfo);
    }


    //根据id查询
    @ResponseBody
    @RequestMapping("/role/get/list/by/id/list")
    public ResultEntity<List<Role>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {
        List<Role> roleListByIdList = roleService.getRoleListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleListByIdList);
    }

    //根据id删除
    @ResponseBody
    @RequestMapping("/role/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {
        roleService.batchRemove(roleIdList);
        return ResultEntity.successWithoutData();
    }


    //t_tole 的增加
    @ResponseBody
    @RequestMapping("/role/save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {
        roleService.saveRole(roleName);
        return ResultEntity.successWithoutData();
    }

    //修改
    @ResponseBody
    @RequestMapping("/role/update/role")
    public ResultEntity<String> updateRole(Role role) {
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }




    @RequestMapping("/assign/to/assign/role/page")
    public String toAssignRolePage(Integer adminId, Integer pageNum, Model model){
        //查询已分配的角色
        List<Role> assignedRoleList = roleService.getAssignedRoleList(adminId);
        //查询未分配的角色
        List<Role>  unAssignedRoleList= roleService.getUnAssignedRoleList(adminId);
        //存储数据
        model.addAttribute("assignedRoleList",assignedRoleList);
        model.addAttribute("unAssignedRoleList",unAssignedRoleList);
        return "assign-role";
    }

    @RequestMapping("/assign/role")
    public String assignRole(@RequestParam(value="roleIdList", required=false) List<Integer> roleIdList,
                             @RequestParam("adminId") Integer adminId,
                             @RequestParam("pageNum") String pageNum){

        //更新关系
        roleService.updateRelationship(adminId,roleIdList);

        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
